<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use Cookie;
use App\Http\Requests;
use App\Models\Approve;
use App\Http\Controllers\Controller;
use App\Http\Controllers\common\YunpianController;

class ApprovalController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        return view('admin.approval.index');
    }

    // 审核
    public function getAudit(Request $request)
    {
        $msg = $request->all();
        if ($msg['status']=='pass') {
            $approve = Approve::where('id',$msg['msg'])->first();
            $datas = $approve->toArray();
            $path = $approve->agent->path;
            $agent = array();
            $agent['pid'] = $datas['agent_id'];
            $agent['name'] = $datas['name'];
            unset($datas['name']);
            $agent['path'] = $path.','.$datas['agent_id'];
            $datas['name'] = $datas['linkman'];
            unset($datas['linkman']);
            $datas['status'] = 1;
            unset($datas['id']);
            $datas['role_id'] = '2';
            $agent_id = DB::table('agent')->insertGetId($agent);
            if ($agent_id) {
                $datas['agent_id'] = $agent_id;
                $datas['role_id'] = DB::table('role')-> insertGetId(['name'=>'总账号','auth_ids'=>'0','area_ids'=>'0','agent_id'=>$agent_id]);
                $abool = DB::table('admin') -> insert($datas);
                if ($abool) {
                    $raw = DB::table('approve')
                        -> where('id',$msg['msg'])
                        -> update(['status'=>$msg['status']]);
                    if ($raw) {
                        $phone = $datas['phone'];
                        $text = '恭喜您通过审核，现已成为我司代理商。现在您可以登录后台，添加、管理你的产品了！';
                        $yunpian = new YunpianController();
                        $yunpian->Msgsend($phone,$text);
                        return 1;
                    } else {
                        return 0;
                    }
                }
                // else {
                    // 写入日志
                // }
            }
            // else {
            //     // 写入日志
            // }
            return 0;
        }else{
            $raw = DB::table('approve')
                -> where('id',$msg['msg'])
                -> update(['status'=>$msg['status']]);
            if ($raw) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        // 
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getShow(Request $request)
    {
        $where = array();
        $where['agent_id'] = Cookie::get('logon')['agent_id'];
        if ($request->input('id')) {
            $where['id'] = $request->input('id');
        }
        $approvals = DB::table('approve')
            -> select (
                'id',
                'name',
                'account',
                'linkman',
                DB::raw(
                    "REPLACE(phone, SUBSTR(phone,4,4), '****') as mobile"
                ),
                'status'
            )
            -> where($where)
            -> get();
            return $approvals;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
